require('./connect')
const mongoose = require('mongoose')

const userSchema = new mongoose.Schema({
  age: Number,
  email: String,
  hobbies: [String],
  name: String,
  password: String,
})
const User = mongoose.model('User', userSchema)

function deleteOne(obj) {
  // 查询结果是一个对象，为空时返回null
  User.findOneAndDelete(obj)
    .then((result) => console.log('删除成功', result))
    .catch((e) => console.log('删除失败', e))
}
// deleteOne({ name: '乐哥哥' })
// deleteOne({ name: '乐哥哥2' })

function deleteMany(obj) {
  /*
  查询结果是{ n: 2, ok: 1, deletedCount: 2 }
  ok: 1 表示删除成功
  n: 2 和 deletedCount: 2 表示成功删除的个数
   */
  User.deleteMany(obj)
    .then((result) => console.log('删除成功', result))
    .catch((e) => console.log('删除失败', e))
}
deleteMany({ age: 29 })
